package kr.hkit.powerjava.chapter17; //예제 연습용!!! 

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.ItemSelectable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.GridLayout;

import javax.swing.JRadioButton;
import javax.swing.JToggleButton;

public class SwingComponentAll extends JFrame implements ItemListener, ActionListener {

	private JPanel contentPane;
	private JPanel panelImage;
	private JPanel panelChkBox;
	private JPanel panelRadioBtn;
	private JLabel lblNewLabel;
	private JButton btnNewButton;
	private JPanel panelChk1;
	private JCheckBox[] chkBtns;
	private JLabel[] lbls;
	private ImageIcon[] icons;
	private JRadioButton rdbtnSmall;
	private JRadioButton rdbtnMedium;
	private JRadioButton rdbtnLarge;
	private JLabel lblNewLabel_2;
	private JPanel panelRadio;
	private JLabel lblNewLabel_3;

	/**
	 * Launch the application.
	 * 
	 */

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SwingComponentAll frame = new SwingComponentAll();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SwingComponentAll() {
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 395);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

		panelImage = new JPanel();
		contentPane.add(panelImage); //
		panelImage.setLayout(new BorderLayout(0, 0));

		lblNewLabel = new JLabel("이미지를 보려면 아래 버튼을 누르세요");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		panelImage.add(lblNewLabel, BorderLayout.CENTER);

		btnNewButton = new JButton("이미지 레이블");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (lblNewLabel.getIcon() == null) {
					ImageIcon icon = new ImageIcon("dog1.jpg");
					lblNewLabel.setIcon(icon);
					lblNewLabel.setText(null);
				} else {
					lblNewLabel.setIcon(null);
					lblNewLabel.setText("이미지를 보려면 아래 버튼을 누르세요");
				}
			}
		});
		panelImage.add(btnNewButton, BorderLayout.SOUTH); 
		
		

		// ============================체크박스

		panelChkBox = new JPanel();
		contentPane.add(panelChkBox);
		panelChkBox.setLayout(new GridLayout(0, 4, 0, 0));

		panelChk1 = new JPanel(new GridLayout(0, 1)); // 체크박스 3개
		panelChkBox.add(panelChk1);


		chkBtns = new JCheckBox[3];
		String[] strs = { "dog1", "dog2", "dog3" };

		lbls = new JLabel[3];
		icons = new ImageIcon[3];

		for (int i = 0; i < 3; i++) {
			chkBtns[i] = new JCheckBox(strs[i]);
			chkBtns[i].addItemListener(this);
			lbls[i] = new JLabel(strs[i] + ".jpg");
			icons[i] = new ImageIcon(strs[i] + ".jpg");
		}
		
		for (int i = 0; i < 3; i++) {
			panelChk1.add(chkBtns[i]);
		}
		
		panelChkBox.add(lbls[0]);
		panelChkBox.add(lbls[1]);
		panelChkBox.add(lbls[2]);
		
	
		
		
		// ====================라디오버튼
		
		panelRadioBtn = new JPanel();
		contentPane.add(panelRadioBtn);
		panelRadioBtn.setLayout(new BorderLayout(0, 0));
		
		lblNewLabel_2 = new JLabel("어떤 크기의 커피를 주문하시겠습니까?");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		panelRadioBtn.add(lblNewLabel_2, BorderLayout.NORTH);
		
		//라디오버튼 패널
		panelRadio = new JPanel();
		panelRadioBtn.add(panelRadio, BorderLayout.CENTER);
		
		Border border = BorderFactory.createTitledBorder("SIZE"); //경계선
		panelRadio.setBorder(border);
		
		rdbtnSmall = new JRadioButton("Small Size");
		rdbtnSmall.addActionListener(this);
		rdbtnMedium = new JRadioButton("Medium Size");
		rdbtnMedium.addActionListener(this);
		rdbtnLarge = new JRadioButton("Large Size");
		rdbtnLarge.addActionListener(this);
		
		ButtonGroup size = new ButtonGroup(); //라디오버튼 그룹
		size.add(rdbtnSmall);
		size.add(rdbtnMedium);
		size.add(rdbtnLarge);
		
		panelRadio.add(rdbtnSmall);
		panelRadio.add(rdbtnMedium);
		panelRadio.add(rdbtnLarge);
		
		
		
		
		lblNewLabel_3 = new JLabel("크기가 선택되지 않았습니다.");
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_3.setForeground(Color.RED);
		panelRadioBtn.add(lblNewLabel_3, BorderLayout.SOUTH);
		
		setVisible(true);
		
	}
	
	// 체크박스용
	@Override
	public void itemStateChanged(ItemEvent e) {

		Object source = e.getItemSelectable();
		for (int i = 0; i < 3; i++) {

			if (source == chkBtns[i]) {
				lbls[i].setIcon(icons[i]);

				if (e.getStateChange() == ItemEvent.DESELECTED) {
					lbls[i].setIcon(null);
				} else {
					lbls[i].setIcon(icons[i]);
				}
			}
		}
	}

	
	//라디오 버튼용 
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == rdbtnSmall) {
			actionPerformedRdbtnSmall(e);
		}
		if (e.getSource() == rdbtnMedium) {
			actionPerformedRdbtnMedium(e);
		}
		if (e.getSource() == rdbtnLarge) {
			actionPerformedRdbtnLarge(e);
		}
	}
	
	protected void actionPerformedRdbtnSmall(ActionEvent e) {
		if (e.getSource() == rdbtnSmall) {
			lblNewLabel_3.setText("Small Size가 선택되었습니다.");
		}
	}
	protected void actionPerformedRdbtnMedium(ActionEvent e) {
		if (e.getSource() == rdbtnMedium) {
			lblNewLabel_3.setText("Medium Size가 선택되었습니다.");
		}
	}
	protected void actionPerformedRdbtnLarge(ActionEvent e) {
		if (e.getSource() == rdbtnLarge) {
			lblNewLabel_3.setText("Large Size가 선택되었습니다.");
		}
	}
}
